package com.scope.customViews;

/**
 * Created by Samrith on 12/2/13.
 */
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ImagePager extends ViewPager {
    public ImagePager(FragmentActivity activity) {
        super(activity.getApplicationContext());
        setBackgroundColor(Color.argb(230, 130, 130, 130));
        mPagingEnabled = true;
        mImages = new ArrayList<Object>();
        mActivity = activity;
        mPagerAdapter = new ImagePagerAdapter();
        setAdapter(mPagerAdapter);
    }

    private FragmentActivity mActivity;
    private PagerAdapter mPagerAdapter;

    private boolean mPagingEnabled;
    private boolean BitmapCollection;
    private List<Object> mImages;

    public final static int INVALID_POSITION = -1;

    public void setImagePaths(Collection<String> path) {
        setImagePaths(path, INVALID_POSITION);
    }

    public void setImagePaths(Collection<String> path, int position) {
    	BitmapCollection = false;
    	mImages.clear();
        mImages.addAll(path);
        mPagerAdapter.notifyDataSetChanged();
        if (position != INVALID_POSITION) {
            setCurrentItem(position, false);
        }
    }
    
    public void setImages(Collection<Bitmap> bmps) {
    	setImages(bmps, INVALID_POSITION);
    }
    
    public void setImages(Collection<Bitmap> bmps, int position) {
    	BitmapCollection = true;
        mImages.clear();
    	mImages.addAll(bmps);
        mPagerAdapter.notifyDataSetChanged();
        if (position != INVALID_POSITION) {
            setCurrentItem(position, false);
        }
    }
    
    public void addImage(Bitmap bmp) throws Exception {
    	BitmapCollection = true;
		mImages.add(bmp);
		mPagerAdapter.notifyDataSetChanged();
        setCurrentItem(mImages.size() - 1, false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.mPagingEnabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.mPagingEnabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        mPagingEnabled = enabled;
    }
    
    public boolean getPagingEnabled() {
    	return mPagingEnabled;
    }
    
    private class ImagePagerAdapter extends PagerAdapter {
        public ImagePagerAdapter() {
            mTempView = null;
        }

        public ImageView mTempView;

        @Override
        public void destroyItem(ViewGroup viewGroup, int position, Object object) {
            mTempView = (ImageView)object;
            viewGroup.removeView((View) object);
        }

        @Override
        public int getCount() {
        	return mImages.size();
        }

        @Override
        public Object instantiateItem(ViewGroup viewGroup, int position) {
            ImageView view;
            if (mTempView == null || mTempView.getParent() != null) {
                view = new ImageView(mActivity.getApplicationContext());
                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        performClick();
                    }
                });
            } else {
                view = mTempView;
            }

            if (BitmapCollection) {
            	view.setImageBitmap((Bitmap)(mImages.get(position)));
            } else {
            	view.setImageDrawable(Drawable.createFromPath((String)(mImages.get(position))));
            }
            viewGroup.addView(view);
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}